package database.team.phase2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import database.team.phase1.RunPhase1;

/* Class RunPhase2
* 1. The pre-sorted Sublists of Phase 1 are stored in the Folder Sublists
* 2. Only the sublists should be stored in the folder Sublists.
* @param  Size of the original file or sum of tuples of the sublist.
* @return a File Merged and Sorted with the same content of the original file
* @throws IOException throws input and output exception  
*/
public class RunPhase2 {
	
	public void processPhase2(int numOfTuples,String subListFolder) throws IOException{
	String absPathSubLists,minElementFile, value,tupleContent;
	int primaryKey;
	int maxValue = 1000000000;
//	File file = new File("SubLists");
	File file = new File(subListFolder);
	//Read all the tmp files from Sublist directory named "intermediateFile+counter.tmp".
	absPathSubLists = file.getAbsolutePath();
	File folder = new File(absPathSubLists);
	File[] tempFiles = folder.listFiles();
	// Counting the number of files (Sublist) in order to start merging.
	int numOfSublist= tempFiles.length;
	int minimum, subList;
	int[] minNumber = new int[numOfSublist];
//	System.out.println("DEBUG: "+numOfSublist);
	String[] minNumberTuple = new String[numOfSublist];
	BufferedReader[] tempFile = new BufferedReader[numOfSublist];
	FileWriter output = new FileWriter("FileSortedAndMerged_"+subListFolder+".txt");
	PrintWriter tuple = new PrintWriter(output);
	//Only if the number of Sublists is greater than 1
	if(numOfSublist > 1){
		//Pointing to the first element of the sublists
		for(int i=0; i < numOfSublist; i++){
			tempFile[i] = new BufferedReader(new FileReader(tempFiles[i]));
		    value = tempFile[i].readLine();
			RunPhase1.phase2_counterIO++;
		    primaryKey = Integer.parseInt(value.substring(0, 7));
		    if (value != null || value.length()> 0)
		    	minNumber[i] = primaryKey;
			   	minNumberTuple[i] = value;
		}
		//Finding the smallest element in the available sublists 
		for (int i = 0; i < numOfTuples; i++){
		   minimum = minNumber[0]; 
		   tupleContent = minNumberTuple[0]; subList = 0;
		   
		    for (int j = 0; j < numOfSublist; j++){
		    	if (minimum > minNumber[j]){
		    		minimum = minNumber[j];
		    		tupleContent = minNumberTuple[j];
		    		subList = j;
		    	}
		    }
		    tuple.println(tupleContent);
		    //RunPhase1.phase2_counterIO++;
		    minElementFile = tempFile[subList].readLine();
		    if(minElementFile !=null){
		    	minNumber[subList] = Integer.parseInt(minElementFile.substring(0,7)); 
		    	minNumberTuple[subList] = minElementFile;
		    }  else{
		    	minNumber[subList] = maxValue;
		    	minNumberTuple[subList] = String.valueOf(maxValue);
		    }
		}
		
		 //Closing the tempFile, tuple and output readers and writers
		 for (int i = 0; i < numOfSublist; i++){
			  RunPhase1.phase2_counterIO++;
			  tempFile[i].close(); tuple.close(); output.close();
		}
	}
	//If there is only one Sublist.
	else{
		tempFile[0] = new BufferedReader(new FileReader(tempFiles[0]));
		for (int i = 0; i < numOfTuples; i++){
		  tupleContent = tempFile[0].readLine();	
		  tuple.println(tupleContent);
			RunPhase1.phase2_counterIO++;
			RunPhase1.phase2_counterIO++;
		}
		tempFile[0].close(); tuple.close(); output.close();
	}
  }
}
		
	